'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' To compile this simple beauty
'   vbc.exe /t:winexe /out:Pada.exe /optimize+ /debug- /win32icon:pada.ico Pada.vb
'       
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Imports System
Imports System.IO
Imports System.Windows
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Reflection

<Assembly: AssemblyTitle("Pada")> 
<Assembly: AssemblyCompany("SDET Consulting Inc.")> 
<Assembly: AssemblyCopyright("License: MS-PL / New BSD")> 
<Assembly: AssemblyVersion("2.0.1.0")> 
<Assembly: AssemblyProduct("Pada")> 

Module __module__pad__a__

    Private __file__ As String   =   "" 

    Sub Main(args() As String)

        If args.Length = 1 AndAlso File.Exists(args(0)) Then __file__ = args(0)           

        Application.Run(New Pada)

    End Sub


    Public Class Pada : Inherits Form

        Private WithEvents TextBox1 As New TextBox()
       
        Private q                   As String   =   ""
        Private numberOfColors      As Integer  =   5
        Private currentColorIndex   As Integer  =   0
        Private numberOfFonts       As Integer  =   5
        Private currentFontIndex    As Integer  =   0
        Private OpacityAccelerator  As Decimal  =   0.04     ' +/- by 4%
        Private position            As Integer  =   0 
        Private DontSaveWhileQuit   As Boolean  =   False
        Private __icon__file__      As String   =   My.Application.Info.DirectoryPath & "\pada.ico"

        Private __diaglog__filter__     As String        =   "Text File|*.txt|Log File|*.log|All|*.*"
        Private __dialog__title__save__ As String        =   "Save your work, save the world!"
        Private __dialog__title__open__ As String        =   "Open the pandora box"

        Private back()  As Color   =   { Color.Black, Color.White, Color.Black, Color.Blue, Color.Blue }
        Private fore()  As Color   =   { Color.GreenYellow, Color.Black, Color.White, Color.Yellow, Color.White }
        Private fonts() As String  =   { "Lucida Console", "Arial", "Verdana", "Tahoma", "Courier New" }

        Sub New
            Initialize
        End Sub


        Sub ClosingWindow Handles Me.Closing 
            If DontSaveWhileQuit Then Return

            If __file__.Length = 0 Then 
                __file__ = System.IO.Path.GetTempPath() & "pada." & String.Format("{0:yyyyMMddhhmmss}", Now())
            End If
                
            WriteToFile
        End Sub


        Sub Keyd(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            ' Open the pandora, see the new world
            If e.Control And e.keycode = keys.O Then 
                Using dialog As New OpenFileDialog
                    dialog.Title  = __dialog__title__open__
                    dialog.Filter = __diaglog__filter__

                    If dialog.ShowDialog = DialogResult.Cancel Then Return

                    __file__         = dialog.FileName

                    SetFormTitle
                    Me.TextBox1.Text = ReadFromFile
                End Using
            End If

            ' Save the work, save the world
            If e.Control And (e.keycode = keys.W Or e.KeyCode = Keys.S) Then 
                If File.Exists(__file__) Then 
                    WriteToFile 
                Else 
                    Using dialog As New SaveFileDialog
                        dialog.Title = __dialog__title__save__
                        dialog.Filter = __diaglog__filter__

                        If dialog.ShowDialog = DialogResult.Cancel Then Return

                        __file__     = dialog.FileName

                        SetFormTitle
                        WriteToFile
                    End Using
                End If
            End If

            ' I'm QUIT
            If e.control And e.keycode = keys.Q Then Me.Close()

            ' Dont give it a dman, just quick, and leave everything behind
            If e.KeyCode = Keys.F12 Then DontSaveWhileQuit = True : Me.Close()

            ' Increase opacity
            If e.control And e.keycode = keys.Up And Me.Opacity * 100 < 100 - OpacityAccelerator Then Me.Opacity += OpacityAccelerator

            ' Decrease opacity
            If e.control And e.keycode = keys.Down And Me.Opacity * 100 > OpacityAccelerator Then Me.Opacity -= OpacityAccelerator

            ' Increase font size
            If e.alt And e.keycode = keys.Up Then Me.TextBox1.Font = New Font(Me.TextBox1.Font.Name, 
                                                                              Me.TextBox1.Font.Size + 1)

            ' Decrease font size
            If e.alt And e.keycode = keys.Down And Me.TextBox1.Font.Size > 1 Then Me.TextBox1.Font = New Font(Me.TextBox1.Font.Name, 
                                                                                                              Me.TextBox1.Font.Size - 1)

            ' Cycle thru the preset back/fore colors
            If e.control And e.shift Then
                currentColorIndex     = IF (currentColorIndex < NumberOfColors - 1, currentColorIndex + 1, 0)
                Me.TextBox1.BackColor = back (currentColorIndex)
                Me.TextBox1.ForeColor = fore (currentColorIndex)
            End If

            ' Cycle thru the preset fonts
            If e.control And e.alt Then
                currentFontIndex = IF(currentFontIndex < NumberOfFonts - 1, currentFontIndex + 1, 0)
                Me.TextBox1.Font = New Font(fonts(currentFontIndex), Me.TextBox1.Font.Size)
            End If

            ' Select 'em all
            If e.Control And e.KeyCode = Keys.A Then Me.TextBox1.SelectAll()

            ' Start a new Pada
            If e.Control And e.KeyCode = keys.N Then System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\pada.exe")

            ' Toggle the wrap
            If e.Control And e.KeyCode = keys.Enter Then Me.TextBox1.wordwrap = Not Me.TextBox1.WordWrap

            ' Search / Find
            If e.Control And e.KeyCode = keys.F Then 
                q = InputBox("Wadaya wanna srch 2day? {ENTER} {I'm (case) sensitive!}")
                position = Me.TextBox1.Text.IndexOf(q)
                If position > 0 Then HighlightTheFound
            End If

            ' Repeat search
            If e.KeyCode = keys.F3 Then
                position = Me.TextBox1.Text.IndexOf(q, position + q.Length)
                If position > 0 Then HighlightTheFound
            End If

            ' Replace
            '        Use sed instead, no replace functionality

            ' Print me out
            ' The support of below code has been dropped by Microsoft VB team ... sigh 
            ' If e.control And e.keycode = keys.P Then My.Computer.Printer.DefaultPrinter.Write(Me.TextBox1.Text)

        End Sub

        Public Function ReadFromFile As String
            Return My.Computer.FileSystem.ReadAllText(__file__)
        End Function

        Public Sub WriteToFile
            My.Computer.FileSystem.WriteAllText(__file__, Me.TextBox1.Text.ToString, False, System.Text.Encoding.ASCII)
        End Sub

        Sub HighlightTheFound
            With Me.TextBox1
                .Select(position, q.Length)
                .ScrollToCaret
            End With 
        End Sub

        Sub SetFormTitle
            Me.Text = If (__file__.Length > 0, "Pada - " & __file__, "Pada")
        End Sub

        Sub Initialize
            
            If File.Exists(__icon__file__) Then Icon = New Icon(__icon__file__)

            Me.Opacity  =  90 / 100     ' set transparency level - Opacity
            Me.Width    =  800          ' use HD 16 x 9 .. rotated HD :)
            Me.Height   =  800 * 9 / 16

            SetFormTitle

            With Me.TextBox1
                .AcceptsReturn =  TRUE
                .AcceptsTab    =  TRUE
                .WordWrap      =  FALSE
                .Multiline     =  TRUE
                .Dock          =  System.Windows.Forms.DockStyle.Fill
                .ScrollBars    =  System.Windows.Forms.ScrollBars.Both
                .BackColor     =  Color.Black
                .ForeColor     =  Color.GreenYellow
                .Font          =  New Font("Lucida Console", 14)
                .Text          =  If(File.Exists(__file__), ReadFromFile, "")
            End With

            Me.Controls.Add(TextBox1)

        End Sub

    End Class 
End Module
